<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
    >
      <el-form-item label="公众号" prop="sys_user_id">
        <el-select
          v-model="queryParams.sys_user_id"
          placeholder="请选择公众号"
          filterable
          clearable
          style="width:150px"
        >
          <el-option
            v-for="customer in customerList"
            :key="customer.id"
            :label="customer.username + '(' + customer.nickname + ')'"
            :value="customer.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分类" prop="cate_id">
        <el-select
          v-model="queryParams.cate_id"
          placeholder="请选择产品"
          style="width:150px"
          filterable
          clearable
        >
          <el-option
            v-for="cate in cateList"
            :key="cate.id"
            :label="cate.name"
            :value="cate.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="面值" prop="face_value">
        <el-input
          placeholder="面值"
          v-model="queryParams.face_value"
          clearable
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="产品" prop="spec_id">
        <el-select
          v-model="queryParams.paramsOrder.spec_id"
          placeholder="请选择产品"
          filterable
          clearable
        >
          <el-option
            v-for="product in productList"
            :key="product.id"
            :label="product.cate?.name + product.face_value"
            :value="product.id"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="渠道" prop="channel_id">
        <el-select
          v-model="queryParams.channel_id"
          placeholder="请选择渠道"
          filterable
          clearable
          style="width:150px"
        >
          <el-option
            v-for="channel in channelList"
            :key="channel.id"
            :label="channel.channel_name"
            :value="channel.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="channel_order_status">
        <el-select
          v-model="queryParams.channel_order_status"
          placeholder="状态"
          clearable
          style="width: 120px"
        >
          <el-option
            v-for="dict in channelOrderStatusList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="recharge_type">
        <el-select
          v-model="queryParams.paramsOrder.recharge_type"
          placeholder="业务类型"
          clearable
          style="width: 100px"
        >
          <el-option label="话费" value="1" />
          <el-option label="流量" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="运营商" prop="isp">
        <el-select
          v-model="queryParams.paramsOrder.isp"
          placeholder="运营商"
          clearable
          style="width: 100px"
        >
          <el-option label="移动" value="1" />
          <el-option label="联通" value="2" />
          <el-option label="电信" value="3" />
        </el-select>
      </el-form-item>

      <el-form-item label="省份" prop="province_id">
        <el-select
          v-model="queryParams.paramsOrder.province_id"
          placeholder="省份"
          clearable
          filterable
          @change="handleProvinceChange"
          @clear="handleProvinceClear"
          style="width: 120px"
        >
          <el-option
            v-for="province in provinceList"
            :key="province.id"
            :label="province.short_name"
            :value="province.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="城市" prop="city_id">
        <el-select
          v-model="queryParams.paramsOrder.city_id"
          placeholder="城市"
          clearable
          filterable
          style="width: 120px"
        >
          <el-option
            v-for="city in cityList"
            :key="city.id"
            :label="city.short_name"
            :value="city.id"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="面值" prop="face_value">
        <el-input
          v-model="queryParams.paramsOrder.face_value"
          placeholder="请输入面值"
          clearable
          style="width: 160px"
        />
      </el-form-item> -->
      <el-form-item label="平台订单号" prop="order_sn">
        <el-input
          v-model="queryParams.order_sn"
          placeholder="请输入平台订单号"
          clearable
          style="width: 260px"
        />
      </el-form-item>
      <el-form-item label="渠道订单号" prop="channel_order_sn">
        <el-input
          v-model="queryParams.channel_order_sn"
          placeholder="请输入渠道订单号"
          clearable
          style="width: 260px"
        />
      </el-form-item>
      <el-form-item label="推送订单号" prop="push_order_sn">
        <el-input
          v-model="queryParams.push_order_sn"
          placeholder="请输入推送订单号"
          clearable
          style="width: 260px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.paramsOrder.mobile"
          placeholder="请输入手机号"
          clearable
          style="width: 160px"
        />
      </el-form-item>
      <el-form-item label="平台订单" prop="is_member_order">
        <el-select
          v-model="queryParams.is_member_order"
          placeholder="平台订单"
          clearable
          style="width: 150px"
        >
          <el-option label="是" value="0" />
          <el-option label="否" value="1" />
        </el-select>
      </el-form-item>
      <!--  -->
      <el-form-item label="创建时间">
        <el-date-picker
          ref="datePicker"
          v-model="dateRange"
          style="width: 380px"
          type="datetimerange"
          range-separator="-"
          value-format="x"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="defaultTime"
        ></el-date-picker>
        <!--  -->
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="Search"
          size="small"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" size="small" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          size="small"
          @click="handleExport"
          v-hasPermi="['api:channelOrder:exportChannelOrder']"
          >导出</el-button
        >
        <!-- v-hasPermi="['api:channelOrder:exportChannelOrderList']" -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Edit"
          size="small"
          :disabled="multiple"
          @click="handleCheckChannelOrder"
          v-hasPermi="['api:channelOrder:checkChannelOrder']"
          >查单</el-button
        >
      </el-col>
      <!-- v-hasPermi="['api:v1:updateChannelOrderStatus']" -->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Edit"
          size="small"
          :disabled="multiple"
          @click="handleMutiStatus"
          v-hasPermi="['api:channelOrder:changeStatus']"
          >批量改状态</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
        <template #default="props">
          <el-form label-position="left" inline class="">
            <el-form-item label="描述">
              <span>{{ props.row.msg }}</span>
            </el-form-item>
            <el-form-item label="提交参数">
              <span>{{ props.row.post_data }}</span>
            </el-form-item>
            <el-form-item label="返回参数">
              <span>{{ props.row.channel_data }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="公众号" align="center" prop="user.username" />
      <el-table-column label="订单号" align="center" prop="order_sn" />
      <el-table-column label="推送订单号" align="center" prop="push_order_sn" />
      <el-table-column
        label="渠道"
        align="center"
        prop="channel.channel_name"
      />
      <el-table-column
        label="渠道订单号"
        align="center"
        prop="channel_order_sn"
      />
      <el-table-column label="手机号" align="center" prop="order.mobile" />
      <el-table-column label="产品" align="center" prop="order.product_name">
        <template #default="scope">
          <span>{{ scope.row.order.goods_full_title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务" align="center" prop="order.recharge_type">
        <template #default="scope">
          <el-tag v-if="scope.row.order.recharge_type == 1">话费</el-tag>
          <el-tag v-if="scope.row.order.recharge_type == 2">流量</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="归属地/运营商" align="center" prop="isp">
        <template #default="scope">
          <span>{{ scope.row.order.area_name }}</span>
          <div v-if="scope.row.order.isp == 1">移动</div>
          <div v-if="scope.row.order.isp == 2">联通</div>
          <div v-if="scope.row.order.isp == 3">电信</div>
        </template>
      </el-table-column>
      <el-table-column
        label="客户/渠道价格"
        align="center"
        prop="channel_price"
      >
        <template #default="scope">
          <el-tag type="warning">{{ scope.row.order.user_price }}</el-tag>
          <br />
          <el-tag type="success">{{ scope.row.channel_price }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="channel_order_status">
        <template #default="scope">
          <el-tag v-if="scope.row.channel_order_status == 1">初始化</el-tag>
          <el-tag type="info" v-if="scope.row.channel_order_status == 2"
            >充值中</el-tag
          >
          <el-tag type="success" v-if="scope.row.channel_order_status == 3"
            >充值完成</el-tag
          >
          <el-tag type="danger" v-if="scope.row.channel_order_status == 4"
            >充值失败</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="平台订单" align="center" prop="is_member_order">
        <template #default="scope">
          <el-tag v-if="scope.row.order.is_member_order == 1" type="warning"
            >否</el-tag
          >
          <el-tag v-else type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建/更新时间"
        align="center"
        prop="created_at"
        width="180"
      >
        <template #default="scope">
          <span>{{ scope.row.created_at }}</span>
          <br />
          <span>{{ scope.row.updated_at }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope"> </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="批量改渠道订单状态"
      v-model="open"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="已选择数量">
          {{ ids.length }}
        </el-form-item>
        <el-form-item label="温馨提示">
          <span style="color: red"
            >请确保没有勾选错订单以及要更改的订单状态!！！</span
          >
        </el-form-item>
        <el-form-item label="修改状态" prop="order_status">
          <el-radio-group v-model="form.order_status">
            <el-radio-button :label="2">充值中</el-radio-button>
            <el-radio-button :label="3">充值完成</el-radio-button>
            <el-radio-button :label="4">充值失败</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="密码"
          v-if="form.order_status == 4"
          prop="password"
        >
          <el-input
            v-model="form.password"
            placeholder="请输入登录密码确认操作"
            type="password"
            show-password
          />
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList as getChannelCzOrderList,
  checkChannelOrder,
  changeStatus as updateChannelOrderStatus,
} from "@/api/platform/channelOrder";
import { getListAll as listAllCzProduct } from "@/api/platform/czProductSpec";
import { getAllList as getCateListAll } from "@/api/platform/czProductCate";

import { getArea2 as getAreas } from "@/api/common";
import { getAllList as listAllCustomer } from "@/api/platform/merchant";
import { getAllList as listAllChannel } from "@/api/channel";

export default {
  name: "platformchannelOrderindex",
  //   dicts: ["cz_product_type", "cz_isp", "cz_order_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      order_ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 字典表格数据
      dataList: [],
      customerList: [],
      productList: [],
      channelList: [],
      channelOrderStatusList: [
        {
          label: "下单成功",
          value: "1",
          list_class: "primary",
        },
        {
          label: "充值中",
          value: "2",
          list_class: "info",
        },
        {
          label: "充值完成",
          value: "3",
          list_class: "success",
        },
        {
          label: "充值失败",
          value: "4",
          list_class: "danger",
        },
      ],
      // 省份列表
      provinceList: [],
      // 省份列表
      cityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      //   [
      //     new Date(new Date().setHours(0, 0, 0, 0) - 3600 * 1000 * 24 * 7),
      //     new Date(),
      //   ],
      dateRange: this.getTimeFilterDefault(true),
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        spec_id: undefined,
        channel_order_status: undefined,
        order_sn: undefined,
        channel_order_sn: undefined,
        push_order_sn: undefined,
        channel_id: undefined,
        /*
        mobile: undefined,
        face_value: undefined,
        province_id: undefined,
        city_id: undefined,
        isp: undefined,
        biz_type: undefined,
        user_order_sn: undefined,
        product_id: undefined,
        */
        paramsOrder: {
          mobile: undefined,
          face_value: undefined,
          province_id: undefined,
          city_id: undefined,
          isp: undefined,
          recharge_type: undefined,
          user_order_sn: undefined,
          spec_id: undefined,
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        order_status: [
          { required: true, message: "请选择状态", trigger: "blur" },
        ],
        is_callback: [
          {
            required: true,
            message: "请选择是否回调订单给客户",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      cateList: [],
    };
  },
  created() {
    this.getCustomerList();
    this.getProvinceList();
    // this.getAllProductList();
    this.getChannelList();
    this.getCateListAll();
    this.getList();
  },
  computed: {
    defaultTime() {
      return [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)];
    },
  },
  methods: {
    async getCateListAll() {
      const { data } = await getCateListAll();
      console.log(data);
      this.cateList = data;
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      const obj = { ...this.addDateRange(this.queryParams, this.dateRange) };
      obj.created_at = [obj.params.beginTime, obj.params.endTime];
      delete obj.params;
      obj.mobile = obj.paramsOrder.mobile;
      obj.city_id = obj.paramsOrder.city_id;
      obj.spec_id = obj.paramsOrder.spec_id;
      obj.isp = obj.paramsOrder.isp;
      obj.province_id = obj.paramsOrder.province_id;
      obj.recharge_type = obj.paramsOrder.recharge_type;
      // delete obj.paramsOrder;
      console.log(111, obj);
      getChannelCzOrderList(obj).then((response) => {
        const data = response.data;
        this.dataList = data.items;
        this.total = data.total;
        this.loading = false;
      });
    },
    getAllProductList() {
      listAllCzProduct().then((response) => {
        this.productList = response.data;
      });
    },
    getCustomerList() {
      listAllCustomer().then((response) => {
        this.customerList = response.data;
      });
    },
    getChannelList() {
      listAllChannel().then((response) => {
        this.channelList = response.data;
      });
    },
    getProvinceList() {
      // this.loading = true;
      getAreas({ level_type: 1 }).then((response) => {
        this.provinceList = response.data;
        // this.loading = false;
      });
    },
    getCityList(id) {
      getAreas({ level_type: 2, parent_id: id }).then((response) => {
        this.cityList = response.data;
      });
    },
    handleProvinceChange(val) {
      if (val) {
        this.getCityList(val);
      }
    },
    handleProvinceClear() {
      this.queryParams.paramsOrder.city_id = undefined;
      this.cityList = [];
    },
    handleMutiStatus() {
      this.reset();
      this.open = true;
    },
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateChannelOrderStatus({ ...this.form, ids: this.ids }).then(
            (response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }
          );
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        order_status: undefined,
        is_callback: undefined,
        remark: undefined,
        password: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = this.getTimeFilterDefault(true);
      this.resetForm("queryForm");
      this.queryParams.paramsOrder = {
        mobile: undefined,
        face_value: undefined,
        province_id: undefined,
        city_id: undefined,
        isp: undefined,
        recharge_type: undefined,
        user_order_sn: undefined,
        spec_id: undefined,
      };
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      // 导出勾选的订单
      const orderIds = this.ids || [];
      console.log(orderIds);
      if (orderIds.length) {
        this.download(
          "api/channelOrder/exportChannelOrder",
          {
            ids: orderIds,
          },
          "渠道订单导出[" + this.parseTime(new Date().getTime()) + "].csv"
        );
      } else {
        const obj = this.addDateRange(this.queryParams, this.dateRange);
        obj.created_at = [obj.params.beginTime, obj.params.endTime];
        delete obj.params;
        this.download(
          "api/channelOrder/exportChannelOrder",
          obj,
          "渠道订单导出[" + this.parseTime(new Date().getTime()) + "].csv"
        );
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.order_ids = selection.map((item) => item.order_id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
    },
    handleCheckChannelOrder() {
      const order_ids = this.ids;
      this.$modal
        .confirm("是否确认要通过接口查询渠道订单[" + order_ids + "]的状态？")
        .then(function () {
          return checkChannelOrder(order_ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作已提交，请晚些再刷新查看。");
        })
        .catch(() => {});
    },
  },
};
</script>
<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 33.3%;
}
</style>
